package org.ddevil.data;



/**
 * A filter that takes a filter string and only accepts objects if their String
 * value starts with the filter string.
 *
 * @author Rich
 */
public class StringFilter implements Filter<Object> {
    String filterString;

    /**
     * Create a new StringFilter that will only accept objects whose String
     * value starts with the given filter string.
     *
     * @param filter
     */
    public StringFilter(String filter) {
        setFilterString(filter);
    }

    public boolean accept(Object arg) {
        String sVal = arg != null ? arg.toString() : "";
        return sVal.startsWith(filterString);
    }

    /**
     * Get the current filter string for this filter.
     *
     * @return
     */
    public String getFilterString() {
        return filterString;
    }

    /**
     * Set the filter string to use for this filter.
     *
     * @param filter
     */
    public void setFilterString(String filter) {
        this.filterString = filter != null ? filter : "";
    }
    
    @Override
    public String toString() {
    	return filterString;
    }
}